# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=flatbuffers
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.5
pkgrel=1
pkgdesc='Memory Efficient Serialization Library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://google.github.io/flatbuffers/'
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-libsystre")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/${_realname}/archive/v${pkgver}.tar.gz"
        "${_realname}-1.9.0.patch"
        "https://github.com/google/flatbuffers/pull/6984.patch"
        "https://github.com/google/flatbuffers/pull/6986.patch")
sha256sums=('b01e97c988c429e164c5c7df9e87c80007ca87f593c0d73733ba536ddcbc8f98'
            '67bf9306dfff8652a81695f96dec8ec0eb6b6e5a57e2b29b4dc0fe5caba16545'
            'd8425c76f36e80a8b6f9d016dd6601631ea9f5fbfc05a0186728f2cb000b78d8'
            'ad870ea35bd8d9956cd8cbdcb593e70cf2a8d505b9a36936b3e9e515f8829846')
options=('strip' '!libtool' 'staticlibs')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/${_realname}-1.9.0.patch"
  patch -Np1 -i "${srcdir}/6984.patch"
  patch -Np1 -i "${srcdir}/6986.patch"

  # Remove Werror flag
  sed -i 's/-Werror=/-W/g;s/-Werror//g' CMakeLists.txt
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DFLATBUFFERS_BUILD_SHAREDLIB=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
  install -Dm755 flatc.exe -t "${pkgdir}${MINGW_PREFIX}"/bin

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
